const { Tab, extend } = require('../../dist/index');
var app=getApp();
Page(extend({}, Tab,{
  data: {
    inputShowed: false,
    showResult: false,
    inputVal: "",
    loading: false,
    nodata: false,
    nomore: false,
    couponList:[],
    sort_method: {
      list: [{
        id: 'commission_rate_desc',
        name: '搜索结果'
      }],
      selectedId: 'commission_rate_desc'
    },
  },
  onLoad: function (res) {
    this.setData({
      inputVal: res.value
    })
    if(res.action=="do"){
      this.setData({
        showResult: true
      })
    this.getMoreCouponList();
    }
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  searchData: function (e) {
    this.setData({
      nodata: false,
      inputVal: e.detail.value,
      showResult: true,
      couponList:[]    
    })
    this.getMoreCouponList();
  },
  //取商品
  getMoreCouponList: function () {
    wx.showLoading({
      title: '搜索中...',
    })
    var that = this
    wx.request({
      url: app.globalData.apiurl,
      data: {
        "a": "so",
        "k": that.data.inputVal,
        "p": that.data.pageIndex
      },
      method: "GET",
      success: function (resRequest) {
       // console.log(resRequest.data.data.result);
        if (resRequest.statusCode === 200) {
          if (resRequest.data.data.result != null && resRequest.data.data.result.length > 0) {
            that.setData({
              couponList: that.data.couponList.concat(resRequest.data.data.result),
            })
            wx.hideLoading()
          }
          else {
            wx.showToast({
              title: '无数据',
              duration:3000
            })
            that.setData({
             nodata:true
            })
          }
        }
      }
    })
  },
}));
